package com.neusoft.databus.common.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.validator.constraints.Length;

import com.neusoft.bizcore.web.jpa.model.AbstractAuditModel;

import lombok.Getter;
import lombok.Setter;

/**
 * 资源实体
 *
 * @author naxiang
 */
@Getter
@Setter
@Entity
//@TypeDefs({ @TypeDef(name = "StringJsonObject", typeClass = StringJsonUserType.class) })
public class Resource extends AbstractAuditModel {

    private static final long serialVersionUID = -7963354466418078563L;

    /** 资源编码唯一标识, 根据ID生成器生成 */
    @NotBlank(message = "资源编码不能为空")
    @Length(max = 255, message = "资源编码不能超过{max}位")
    @Column(length = 255, nullable = false, unique = true)
    private String code;

    /** 资源名称 */
    @NotBlank(message = "资源名称不能为空")
    @Length(max = 255, message = "资源名称不能超过{max}位")
    @Column(length = 255, nullable = false)
    private String name;

    /** 禁用标识 */
    private boolean disabled = false;

    /** 资源配置属性 */
    @Length(max = 1023, message = "资源配置属性不能超过{max}位")
    @Column(length = 1023)
    private String config;

    /** 资源类型 */
    @Length(max = 255, message = "资源类型不能超过{max}位")
    @Column(length = 255)
    private String type;

    /** 所属网关 */
    @ManyToOne
    private Gateway gateway;

    /** 关联指标组。维护关系方 */
    @ManyToMany(targetEntity = MetricGroup.class, cascade = {CascadeType.MERGE, CascadeType.PERSIST })
    @JoinTable(name = "ref_resource_metric_group", joinColumns = {
            @JoinColumn(name = "resouce_id", referencedColumnName = "id") }, inverseJoinColumns = {
                    @JoinColumn(name = "metric_group_id", referencedColumnName = "id") })
    @LazyCollection(LazyCollectionOption.TRUE)
    private List<MetricGroup> metricGroups = new ArrayList<>();

    @Length(max = 255, message = "子系统关联关系不能超过{max}位")
    @Column(length = 255)
    private String associate;

    /** 注释 */
    @Length(max = 1023, message = "注释不能超过{max}位")
    @Column(length = 1023, nullable = true)
    private String comment;

    /**
     * 采集周期(任务调度用)
     */
    @NotNull(message = "采集周期不能为空")
    @Min(value = 1, message = "采集周期不能小于{value}秒")
    @Column(nullable = false)
    private Long executionCycle = 60L;// 指标组的采集周期
}
